package com.mrd.frame.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.frame.dao.ConfigDao;
import com.mrd.frame.dao.WhiteListConfigDao;
import com.mrd.frame.domain.Config;
import com.mrd.frame.domain.WhiteListConfig;
import com.mrd.frame.redis.JedisClientClusterImpl;
import com.mrd.frame.service.ConfigService;
import com.mrd.frame.service.WhiteListConfigService;

@Service
public class WhiteListConfigServiceImpl implements WhiteListConfigService {
	@Autowired
	private WhiteListConfigDao whiteListConfigDao;
	@Autowired
	private ConfigService configService;
	@Autowired
	private JedisClientClusterImpl jedisClientClusterImpl;
	@Autowired
	private ConfigDao configDao;
	
	@Override
	public int deleteByPrimaryKey(String whiteListConfigId) {
		return whiteListConfigDao.deleteByPrimaryKey(whiteListConfigId);
	}

	@Override
	public int insert(WhiteListConfig record) {
		String userKey = record.getUserKey();
		if(userKey == null){
			return -1;
		}else if(configService.selectByUserKey(userKey) == null){
			Config config = new Config();
			config.setUserKey(userKey);
			config.setStatus("1");
			configService.insert(config);
		}
		int i = whiteListConfigDao.insert(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public int insertSelective(WhiteListConfig record) {
		String userKey = record.getUserKey();
		if(userKey == null){
			return -1;
		}else if(configService.selectByUserKey(userKey) == null){
			Config config = new Config();
			config.setUserKey(userKey);
			config.setStatus("1");
			configService.insert(config);
		}
		int i = whiteListConfigDao.insertSelective(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public WhiteListConfig selectByPrimaryKey(String whiteListConfigId) {
		return whiteListConfigDao.selectByPrimaryKey(whiteListConfigId);
	}

	@Override
	public int updateByPrimaryKeySelective(WhiteListConfig record) {
		int i = whiteListConfigDao.updateByPrimaryKeySelective(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(record.getUserKey());
			jedisClientClusterImpl.set("config_" + record.getUserKey(), config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public int updateByPrimaryKey(WhiteListConfig record) {
		int i = whiteListConfigDao.updateByPrimaryKey(record);
		if(i > 0){
			Config config = configDao.selectByUserKey(record.getUserKey());
			jedisClientClusterImpl.set("config_" + record.getUserKey(), config, null, TimeUnit.MILLISECONDS);
		}
		return i;
	}

	@Override
	public int deleteByPrimaryKeys(String ids) {
		String[] id = ids.split(",");
		return whiteListConfigDao.deleteByPrimaryKeys(id);
	}

	@Override
	public List<WhiteListConfig> selectSelective(WhiteListConfig whiteListConfig) {
		return whiteListConfigDao.selectSelective(whiteListConfig);
	}
}